package home.my.base.util.downloader;

import home.my.base.model.BaseObj;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

public abstract class CsvDownLoader<Pojo extends BaseObj> implements DownLoader<Pojo>{
	//导出分隔符
	public final static String SPLIT = ",";

	private HttpServletResponse response;
	
	public CsvDownLoader(HttpServletResponse response) {
		this.response = response;
	}

	public abstract String buildTitle();

	public abstract String buildCsvRow(Pojo pojo);

	@Override
	public void handleRow(Pojo pojo) {
		try {
			response.getWriter().println(buildCsvRow(pojo));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String replaceChars(String content){
		StringBuilder sb = new StringBuilder();
		sb.append("\"");
		sb.append( StringUtils.isNotBlank(content) ? content.replaceAll("\"", "\"\"") : "" );
		sb.append("\"");
		return sb.toString();
	}

}
